<!DOCTYPE html>
<html>

<head>
    <title>Web worker</title>
</head>

<body>
    <h1>web worker demo</h1>

    <button id="request">请求</button>
    <input type="text" id="result" />
    <script>
        document.getElementById('request').addEventListener('click', doing);

        function doing() {
            var worker = new Worker('webworker.js');
            worker.postMessage({
                a: 1,
                b: 2
            });
            worker.onmessage = function (event) {
                console.log('Received message ' + JSON.stringify(event.data));
                document.getElementById('result').value = event.data;
                doSomethingElse();
            };

            function doSomethingElse() {
                // 执行任务
                // worker.postMessage('Work done!');
            }
        }
    </script>
</body>

</html>